package com.zxk.service.impl;

import com.sun.jna.platform.win32.WinDef;
import com.zxk.constant.JNAConstants;
import com.zxk.constant.PicConstants;
import com.zxk.entity.FindPicResult;
import com.zxk.enums.Coord;
import com.zxk.service.AbstractMyTask;
import com.zxk.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 刮刮乐任务
 *
 * @author: 舟小亢
 * @date: 2023-08-30 19:17
 */
public class ScratchTicketTaskImpl extends AbstractMyTask {

    private static Logger LOG = LoggerFactory.getLogger(ScratchTicketTaskImpl.class);

    public ScratchTicketTaskImpl(WinDef.HWND hwnd_0, WinDef.HWND hwnd_1, WinDef.HWND hwnd_2) {
        super(hwnd_0, hwnd_1, hwnd_2);
    }

    @Override
    public String getTaskName() throws Exception {
        return "刮刮乐";
    }

    @Override
    public boolean execute() throws Exception {
        LOG.info("【刮刮乐任务】开始！");

        // 判断每日福利界面是否已打开
        if (!FuzzyPicUtil.findPic(hwnd_0, PicConstants.刮刮乐任务_每日福利, 3).isFinded()) {
            // 找到福利并打开
            if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.公共_福利, 0.6)) {
                return false;
            }
        }

        // 找到每日福利并打开
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.刮刮乐任务_每日福利)) {
            return false;
        }

        // 检查完成状态
        pic = FuzzyPicUtil.findPicM(hwnd_0, new String[]{PicConstants.刮刮乐任务_已完成, PicConstants.刮刮乐任务_未完成});
        if (pic.getFindResult()[0] == 1) {
            LOG.warn("【刮刮乐任务】已完成，直接开始祈福！");
            // 进入界面
            if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.刮刮乐任务_打开)) {
                return false;
            }

        } else {
            if (pic.getFindResult()[1] != 1) {
                LOG.error("【刮刮乐任务】未找到完成状态，退出！");
                return false;
            }

            if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.刮刮乐任务_打开)) {
                return false;
            }

            ThreadUtil.sleep1();
            LOG.info("【刮刮乐任务】开始刮奖！");
            // 确认是否刮开
            if (!FuzzyPicUtil.findPic(hwnd_0, PicConstants.刮刮乐任务_刮完了, 3).isFinded()) {
                MouseUtil.dragMove(hwnd_1, 500, 330, 300, 0);
                MouseUtil.dragMove(hwnd_1, 500, 345, 300, 0);
                MouseUtil.dragMove(hwnd_1, 500, 360, 300, 0);
                MouseUtil.dragMove(hwnd_1, 500, 375, 300, 0);
                MouseUtil.dragMove(hwnd_1, 500, 390, 300, 0);
                MouseUtil.dragMove(hwnd_1, 500, 405, 300, 0);
            } else {
                LOG.warn("【刮刮乐任务】已经刮开过！");
            }

        }

        // 召唤灵祈福
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.刮刮乐任务_召唤灵祈福)) {
            return false;
        }

        // 判断完成情况
        pic = FuzzyPicUtil.findPicM(hwnd_0, new String[]{PicConstants.刮刮乐任务_接受任务, PicConstants.刮刮乐任务_祈福已完成过});
        if (!pic.isFinded()) {
            // 一个未找到，退出当前任务
            return false;
        }
        if (pic.getFindResult()[1] == 1) {
            LOG.error("【刮刮乐任务】已完成过祈福任务，退出！");
            // 已完成过任务，摁ESC，然后退出
            KeyUtil.kClick(hwnd_1, JNAConstants.VK_ESCAPE);
            return false;
        }

        // 接受任务
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.刮刮乐任务_接受任务, 0.95)) {
            return false;
        }

        // 默认刮第一个
        ThreadUtil.sleep2();
        // 根据线程名确认刮哪个
        String hwndName = Thread.currentThread().getName();
        String pic = PicConstants.刮刮乐任务_迟不得;
        if (hwndName.contains("mhxy003")) {
            pic = PicConstants.刮刮乐任务_龙女;
        } else if (hwndName.contains("mhxy004")) {
            pic = PicConstants.刮刮乐任务_野鬼;
        }
        if (!scratchPetPray(pic) && !scratchPetPray(PicConstants.刮刮乐任务_任意宠物)) {
            return false;
        }

        // 确定
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.刮刮乐任务_确定)) {
            return false;
        }

        // 检查是否完成
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.刮刮乐任务_祈福结束)) {
            return false;
        }

        // 退出
        ThreadUtil.sleep2();
        KeyUtil.kClick(hwnd_1, JNAConstants.VK_ESCAPE);
        KeyUtil.kClick(hwnd_1, JNAConstants.VK_ESCAPE);

        LOG.info("【刮刮乐任务】完成！");
        return true;
    }

    @Override
    public boolean checkTask() {
        return true;
    }

    private boolean scratchPetPray(String pic) throws Exception {
        LOG.info("【刮刮乐任务】祈福宠物是{}", pic);
        for (int i = 0; i < 5; i++) {
            if (FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, pic, 3)) {
                LOG.info("【刮刮乐任务】找到祈福宠物");
                // 双重点击
                FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, pic, 3);
                return true;
            }
            MouseUtil.dragMove(hwnd_1, Coord.PET_PRAY_WHELL.getX(), Coord.PET_PRAY_WHELL.getY(), 0, -200);
        }
        return false;
    }
}
